<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{label: 'name'}"
      default-expand-all
      show-checkbox
      :check-strictly="true"
      node-key="id"
    />
    <!-- :check-strictly="true" 是否关闭父子关联    同等于 check-strictly
         show-checkbox  是否开启选择框默认不写为false  同等于 :show-checkbox="true"
         default-expand-all  展开树形结构   同等于 : default-expand-all="true"
     -->

    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="hCancel">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 存储权限数据
    }
  },
  created() {
    this.locaPermissionList()
    this.loadRoleDetail()
  },
  methods: {

    // 获取所有权限
    async locaPermissionList() {
      const res = await getPermissionList()
      //   console.log(res.data)
      this.list = tranListToTreeData(res.data)
    },

    // 获取权限详情
    async loadRoleDetail() {
      const res = await getRoleDetail(this.roleId)
      console.log(res.data.permIds)
      // 数据回填
      //   this.$refs.refTree.setCheckedKeys(res.data.permIds)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    // 确认按钮
    async hSubmit() {
      // getCheckedKeys() 获取树形结构节点中被选择的节点
      const permIds = this.$refs.refTree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds })
      //   console.log(res)
      this.$message.success('分配权限成功')
      this.$emit('close')
    },
    // 取消按钮
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
